package com.example.ycdyus.dailyfruit.fragment;


import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ycdyus.dailyfruit.BaseApp;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.constant.Variable;
import com.example.ycdyus.dailyfruit.db.User;
import com.example.ycdyus.dailyfruit.utils.ImageLoaderHelper;
import com.lidroid.xutils.exception.DbException;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_my)
public class MyFragment extends Fragment  implements View.OnClickListener{
    @ViewById
    ImageView ivMyIconImg;
    @ViewById
    TextView tvMyJifen;
    @ViewById
    TextView tvMyYue;
    @ViewById
    TextView tvMyYouhuiquan;
    @ViewById
    GridView gvMyGridCategory;
    @ViewById
    TextView tvMyLogin;
//    @ViewById
//    TextView tvMyRegister;

    public SimpleAdapter adapter;
    public List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

    private DisplayImageOptions options;

    @AfterViews
    public void init(){
        options = ImageLoaderHelper.getDisplayImageLoader();
        tvMyLogin.setOnClickListener(this);

        for (int i = 0; i < 6; i++) {
            Map<String,Object> map =new HashMap<>();
            map.put("img", Variable.MY_GRID_IMGS[i]);
            map.put("des",Variable.MY_GRID_TEXT[i]);
            list.add(map);
        }
        adapter = new SimpleAdapter(getActivity(),list,R.layout.my_grid,new String[]{"img","des"},new int[]{R.id.iv_item_my_grid,R.id.tv_item_my_grid});
        gvMyGridCategory.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_my_login:
//                Intent intent = new Intent(getActivity(), LoginActivity_.class);
//                startActivityForResult(intent, AppConfig.REQUEST_CODE);
//                startActivity(intent);
                threeLogin(ShareSDK.getPlatform(QQ.NAME));
                break;
//            case R.id.tv_my_register:
//                Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
//                break;
        }
    }

    /**
     * 第三方登陆实现方法
     * 1> 获取平台
     */
    public void threeLogin(Platform platform) {
//        platform.removeAccount();
//        platform.removeAccount(true);
        if (platform == null) {
            return;
        }
        PlatformDb db = platform.getDb();
        if (db.isValid()) {
            //一种已经认证过并且没过期
            String id = db.getUserId();
            String name = db.getUserName();
            String url = db.getUserIcon();
            ImageLoader.getInstance().displayImage(url, ivMyIconImg, options);
            tvMyLogin.setText(name);
            User user = new User();
            user.setName(name);
            user.setPsw(String.valueOf(name.hashCode()));
            try {
                BaseApp.getInstance().getDbUtils().save(user);
            } catch (DbException e) {
                e.printStackTrace();
            }
            return;
        }

        /**
         *没有认证
         */
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                String name = (String) hashMap.get("nickname");
                String userIcon = (String) hashMap.get("figureurl_qq_2");
                String id = System.currentTimeMillis()+"";
                tvMyLogin.setText(name);
                User user = new User();
                user.setName(name);
                user.setPsw(String.valueOf(name.hashCode()));
                try {
                    BaseApp.getInstance().getDbUtils().save(user);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Toast.makeText(getActivity(), "验证失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Toast.makeText(getActivity(), "取消验证", Toast.LENGTH_SHORT).show();
            }
        });
        platform.showUser(null);
    }
}
